var scene,
    camera,
    renderer;

//场景初始化
function init() {
    scene = new THREE.Scene(); //初始化场景

    //透视相机 可以远近高低各不同
    camera = new THREE.PerspectiveCamera(
        90,
        document.body.clientWidth / document.body.clientHeight,
        0.1,
        100
    );
    camera.position.set(0, 0, 3);
    //opengl在web的实现 3d图形 计算机图形学
    renderer = new THREE.WebGLRenderer({
        antialias: true //抗锯齿
    });
    renderer.setSize(
        document.body.clientWidth,
        document.body.clientHeight,
    )
    document
        .getElementById('container')
        .appendChild(renderer.domElement)

    var controls = new THREE.OrbitControls(camera, renderer.domElement);
    loadCarModal();
    addLight();
    loop();
}

function addLight() {
    var ambientLight = new THREE.AmbientLight(0xd5d5d5);
    ambientLight.intensity = 1.2;
    scene.add(ambientLight);
}

function loadCarModal() {
    //加一个loader.js
    const loader = new THREE.GLTFLoader();
    loader.load(
        './images/model.gltf',
        function(gltf) {
            //成功加载后   
            scene.add(gltf.scene);
        },
        function(xhr) {
            console.log((xhr.loaded / xhr.total * 100) + '%loaded');
        },
        function(error) {
            console.log('An error happened');
        }
    )
}

function loop() {
    //申请一下显卡的运动帧  显卡的显示频率
    requestAnimationFrame(loop);
    renderer.render(scene, camera)
}
window.onload = init;